<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of staffController
 *
 * @author vampired
 */
class Admin_StaffController extends Zend_Controller_Action {

    public function indexAction() {
//        $this->view->show = StaffTable::getPageData('audio', $_GET['page']);
        $this->view->staff = StaffTable::getStaff();
        $this->view->root = ROOT_PATH;
        $aMessages = $this->_helper->FlashMessenger->getMessages();
        
    }

    public function addAction() {

        $form = new Golib_Form_Staff();

        $this->view->form = $form;
        if ($this->_request->isPost() && $form->isValid($_POST)) {

            $doctor = new Staff();
            $doctor->title = $_POST['title'];
            $doctor->name = $_POST['name'];
            $doctor->speciality = $_POST['speciality'];
            $doctor->about = $_POST['about'];
            $doctor->created_at = date("Y-m-j h:i:s");
            if ($form->file->isUploaded()) {
                $originalFilename = pathinfo($form->file->getFileName());

                $doctor->image = 'doctor-' . uniqid() . '.' . $originalFilename['extension'];

                $form->file->addFilter('Rename', $doctor->image);
                $form->file->receive();
            }
            $saved = $doctor->trySave();
            //to add some message or something
            $this->view->saved = $saved;
        
            $this->_redirect('/admin/staff');
        }
    }

    /*
     * 
     * THIS UPDATE NAME ONLY
     * STOP WORK TILL SEE IF IT REQUIRED
     * 
     * 
     */

    public function updateAction() {
        $this->_helper->viewRenderer('add');
        $form = new Golib_Form_Staff();
        $update = $form->getElement('submit');
        $update->setLabel('adjust');
        if ($this->_request->isGet() && $_GET['id']) {
            $doctor = StaffTable::getInstance()->findOneBy('id', $_GET['id']);
            $elmId = new Zend_Form_Element_Hidden('id');
            $elmId->setValue($_GET['id']);
            $form->addElement($elmId);
        } elseif ($this->_request->isPost()) {
//            var_dump($_POST);die();
            $elmId = new Zend_Form_Element_Hidden('id');
            $elmId->setValue($_POST['id']);
            $form->addElement($elmId);
            $doctor = StaffTable::getInstance()->findOneBy('id', $_POST['id']);
            $doctor->title = $_POST['title'];
            $doctor->name = $_POST['name'];
            $doctor->speciality = $_POST['speciality'];
            $doctor->about = $_POST['about'];
            if ($form->file->isUploaded()) {
                $originalFilename = pathinfo($form->file->getFileName());

                $doctor->image = 'doctor-' . $_POST['id'] . '-' . uniqid() . '.' . $originalFilename['extension'];

                $form->file->addFilter('Rename', $doctor->image);
                $form->file->receive();
            }
            $doctor->replace();
            $this->_redirect('/admin/staff');
        }
//        var_dump($audio);die();
        $form->getElement('title')->setValue($doctor['title']);
//        var_dump($audio->level);die();
        $form->getElement('name')->setValue($doctor->name);
        $form->getElement('speciality')->setValue($doctor->speciality);
        $form->getElement('about')->setValue($doctor->about);
        $this->view->form = $form;
    }

    public function deleteAction() {
        $doctor = new Staff();
        $doctor->assignIdentifier($_GET['id']);
        $doctor->delete();
        $this->_helper->FlashMessenger('data removed');
        $this->_redirect('/admin/staff');
    }

}

